package cn.iocoder.an.module.business.controller.admin.equipmentcheckrecorddetail;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckrecorddetail.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckrecorddetail.EquipmentCheckRecordDetailDO;
import cn.iocoder.an.module.business.service.equipmentcheckrecorddetail.EquipmentCheckRecordDetailService;

@Tag(name = "管理后台 - 点检记录明细")
@RestController
@RequestMapping("/business/equipment-check-record-detail")
@Validated
public class EquipmentCheckRecordDetailController {

    @Resource
    private EquipmentCheckRecordDetailService equipmentCheckRecordDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建点检记录明细")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:create')")
    public CommonResult<Long> createEquipmentCheckRecordDetail(@Valid @RequestBody EquipmentCheckRecordDetailSaveReqVO createReqVO) {
        return success(equipmentCheckRecordDetailService.createEquipmentCheckRecordDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点检记录明细")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:update')")
    public CommonResult<Boolean> updateEquipmentCheckRecordDetail(@Valid @RequestBody EquipmentCheckRecordDetailSaveReqVO updateReqVO) {
        equipmentCheckRecordDetailService.updateEquipmentCheckRecordDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除点检记录明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckRecordDetail(@RequestParam("id") Long id) {
        equipmentCheckRecordDetailService.deleteEquipmentCheckRecordDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除点检记录明细")
                @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckRecordDetailList(@RequestParam("ids") List<Long> ids) {
        equipmentCheckRecordDetailService.deleteEquipmentCheckRecordDetailListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得点检记录明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:query')")
    public CommonResult<EquipmentCheckRecordDetailRespVO> getEquipmentCheckRecordDetail(@RequestParam("id") Long id) {
        EquipmentCheckRecordDetailDO equipmentCheckRecordDetail = equipmentCheckRecordDetailService.getEquipmentCheckRecordDetail(id);
        return success(BeanUtils.toBean(equipmentCheckRecordDetail, EquipmentCheckRecordDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得点检记录明细分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:query')")
    public CommonResult<PageResult<EquipmentCheckRecordDetailRespVO>> getEquipmentCheckRecordDetailPage(@Valid EquipmentCheckRecordDetailPageReqVO pageReqVO) {
        PageResult<EquipmentCheckRecordDetailDO> pageResult = equipmentCheckRecordDetailService.getEquipmentCheckRecordDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentCheckRecordDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点检记录明细 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentCheckRecordDetailExcel(@Valid EquipmentCheckRecordDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentCheckRecordDetailDO> list = equipmentCheckRecordDetailService.getEquipmentCheckRecordDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点检记录明细.xls", "数据", EquipmentCheckRecordDetailRespVO.class,
                        BeanUtils.toBean(list, EquipmentCheckRecordDetailRespVO.class));
    }

}